if ("`swdLocal'\Data"=="") {
	di as error "Please run init.do first."
	error 1
}
clear all
set more off
set maxvar 10000

*Open file for regressions
use "${gsdAnalysisOutput}/bscie_panel_indices_final", clear

*Keep only endline and lagged controls
keep if wave==2 & final_sample==1
drop if replacement == 1 & gender == 1

*Define strata
egen strata=group(gender state_old)

*Generate treatment streams
gen treatmentX=0
replace treatmentX=1 if treatment_stream==3

ren edu_level_b  edu_b

*Generate IV treatment x kcb_dist 
gen Z_treat_kcb=treatment*lg_kcb_dist
label var Z_treat_kcb "Treatment x (log) distance to KCB branch"

*Generate interaction for other geographic controls
gen treat_citydist=treatment*lg_city_dist
label var treat_citydist "Treatment x (log) distance to city center"
gen treat_roaddist=treatment*lg_road_dist
label var treat_roaddist "Treatment x (log) distance to primary road"
gen treat_gradient=treatment*gradient
label var treat_gradient "Treatment x gradient"
gen treat_alt=treatment*info_loc_gps_altitude_old
label var treat_alt "Treatment x altitude"
gen treat_conflict = treatment*death_x_prox_a300
label var treat_conflict "Treatment x conflict_affected_index(300km buffer)"
gen treat_edu = treatment*edu_b
gen treat_num = treatment*num_eval_b
gen treat_lit = treatment*lit_eval_b

*Define geographic controls
global geo_controls lg_city_dist treat_citydist treat_roaddist lg_road_dist gradient treat_gradient death_x_prox_a300 treat_conflict 

global base_controls i.edu_b i.treat_edu i.num_eval_b i.treat_num i.lit_eval_b i.treat_lit

*Derive the first-stage partial R^2
ivregress 2sls employment_index lg_kcb_dist treatment (treatmentX= Z_treat_kcb) , cluster(boma_old)
estat firststage, all
ivregress 2sls employment_index lg_kcb_dist treatment i.strata $geo_controls (treatmentX= Z_treat_kcb) , cluster(boma_old)
estat firststage, all
ivregress 2sls employment_index lg_kcb_dist treatment i.strata $geo_controls $base_controls (treatmentX= Z_treat_kcb) , cluster(boma_old)
estat firststage, all

*** First stage results ********************************************************
	
			eststo: xi: ivreg2 employment_index lg_kcb_dist treatment ( treatmentX= Z_treat_kcb) , cluster(boma_old) first savefirst savefprefix(fstage_) 
			est restore fstage_treatmentX 
			outreg2 using "${gsdTables}/Table2.tex" , tex p sdec(3) bdec(3) nocons label  replace keep(treatment Z_treat_kcb lg_kcb_dist)	
			
		eststo: xi: ivreg2 employment_index lg_kcb_dist treatment $geo_controls  i.strata ( treatmentX= Z_treat_kcb) , cluster(boma_old) first savefirst savefprefix(fstage_) 		
			est restore fstage_treatmentX 
			outreg2 using "${gsdTables}/Table2.tex", tex p sdec(3) bdec(3) nocons label  append keep(treatment Z_treat_kcb lg_kcb_dist)
		
	eststo: xi: ivreg2 employment_index lg_kcb_dist treatment $geo_controls $base_controls  i.strata (treatmentX= Z_treat_kcb) , cluster(boma_old) first savefirst savefprefix(fstage_) 		
			est restore fstage_treatmentX 
			outreg2 using "${gsdTables}/Table2.tex", tex p sdec(3) bdec(3) nocons label  append keep(treatment Z_treat_kcb lg_kcb_dist)
			
